package edu.ycxy.poetry.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ycxy.poetry.entity.User;
import edu.ycxy.poetry.mapper.UserMapper;
import edu.ycxy.poetry.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author @xin
 * @createDate 2022/4/26
 * @Description 用户操作的业务逻辑操作
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(User user) {
        //调用userMapper的方法中的findByEmail方法，根据email查询用户的参数
        User u  = userMapper.findByEmail(user.getEmail());
        // 当查询结果为空时，表示该对象不存在
        if (u == null){
            return "该用户不存在";
        }
        // 当用户存在的时候，核实密码是否正确
        if (u.getPassword().equals(user.getPassword())){
            user.setEmail(u.getEmail());
            user.setPassword(u.getPassword());
            // 当都正确的时候
            return "ok";
        }
        return "密码不正确";

    }

    // 更新新用户信息
    @Override
    public void updateInfo(User user) {
        //查询用户是否存在
        User u = userMapper.findByEmail(user.getEmail());
        if (u!= null){
//            // 获取id
//            Integer id = u.getId();
//            u.setUsername(user.getUsername())
//                    .setEmail(user.getEmail())
//                    .setNickname(user.getNickname())
//                    .setAddress(user.getAddress())
//                    .setCity(user.getCity())
//                    .setCountry(user.getCountry())
//                    .setZipCode(user.getZipCode())
//                    .setZipCode(user.getZipCode());
//            Integer update = userMapper.update(u);
            Integer update = userMapper.update(user);
            if (update == 1){
                System.out.println("更新成功");
            }
        }
    }

    @Override
    public User findInfo(User user) {
        return userMapper.findInfo();
    }
}
